﻿using System.Windows;
using System.Windows.Media;

namespace VisualView
{
    /// <summary>
    /// DrawingVisual显示控件
    /// </summary>
    public class DrawingVisualView : FrameworkElement
    {
        #region 显示内容
        protected override int VisualChildrenCount { get { return Visual == null ? 0 : 1; } }
        protected override Visual GetVisualChild(int index)
        {
            return this.Visual;
        }

        /// <summary>
        /// 显示的内容
        /// </summary>
        public DrawingVisual Visual
        {
            get { return (DrawingVisual)GetValue(VisualProperty); }
            set { SetValue(VisualProperty, value); }
        }
        public static readonly DependencyProperty VisualProperty = DependencyProperty.Register("Visual", typeof(DrawingVisual), typeof(DrawingVisualView), new PropertyMetadata((d, e) =>
        {
            if (d is DrawingVisualView v) v.VisualChanged(e.OldValue, e.NewValue);
        }));
        private void VisualChanged(object oldvisual, object newvisual)
        {
            if (oldvisual != null && oldvisual is Visual ov) base.RemoveVisualChild(ov);
            if (newvisual != null && newvisual is Visual nv) base.AddVisualChild(nv);
        }
        #endregion

        public DrawingVisualView()
        {
            base.ClipToBounds = true;
        }
    }
}
